import {
    initPage,
    getTenantId,
    ConstValues,
    getStopworkById,
    formatPostData,
    saveStopWorkBill
} from './../projectIndicators.config';

import { getPrintCodeByType } from 'utils/netRequest.js';

import moment from 'moment';
moment.locale('zh-cn');

export default function buttonClick({ key }) {
    switch (key) {
        case 'add':
            this.setState({ pageStatus: 'edit', result: '' });
            break;
        case 'save':
            // 必须项校验
            let validateFormResult = this.props.form.validateForm();
            if (validateFormResult.length != 0) {
                this.props.vendorsDanger({ 'text': validateFormResult.join(',') + '为必输项' });
                return;
            }
            let formData = this.props.form.collectFormValue();
            let postData = formatPostData(formData);
            saveStopWorkBill(postData).then((res) => {
                let { resultCode, result, exMsg } = res.data;
                if (resultCode === 'ok') {
                    let assignFlag = this.props.approve.initApproveModal(result); // 指派
                    if (assignFlag) {
                        initPage.call(this, { result, pageStatus: 'browse' });
                    }
                } else {
                    this.props.vendorsDanger({ text: exMsg })
                }
            });
            break;
        case 'cancel':
            window.location.href = './stopworklist.html';
            break;
        case 'approve':
            this.props.passreject.initPassRejectModal({ record: this.state.result, page: 'card', primaryKey: ConstValues.Primarykey })
            break;
        case 'attachment':
            this.ygcUpload.open();
            break;
        case 'approveDes':
            this.props.approveHistory.initModal({ data: this.state.result, primaryKey: ConstValues.Primarykey });
            break;
        case 'print':
            getPrintCodeByType({
                tenantId: getTenantId(),
                billType: 'TGBG',
                pkBill: this.state.result[ConstValues.Primarykey],
            });
            break;
        case 'refresh':
            getStopworkById.call(this, {
                record: this.state.result, callback: (res) => {
                    let { resultCode, result, exMsg } = res.data;
                    if (resultCode === 'ok') {
                        initPage.call(this, { result, pageStatus: 'browse' });
                        this.props.vendorsSuccess({ text: '刷新成功！' });
                    } else {
                        this.props.vendorsDanger({ text: exMsg });
                    }
                }
            })
            break;
        default:
            break;
    }
}